package cn.nexd.app;

import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.os.Vibrator;

import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.nexd.app.model.ExpandableModel;
import cn.nexd.app.model.MallModel;
import cn.nexd.app.model.UserInfo;
import cn.nexd.app.orm.LiteOrm;
import cn.nexd.app.util.FileUtil;
import cn.nexd.app.util.MySqlLiteOpenHelper;

/**
 * Created by codngfish on 16/2/1.
 */
public class NexdApplication extends Application {
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm", Locale.CHINA);

    public static final String APPKEY = "f4b5aeaa1776f7af714e0663fa2e50b2";

    public static String userName = "zyc";// 用户名
    public static String userEmial = "zyc@nexdtech.com"; // 用户账号,邮箱
    public static String userPassword = ""; // 用户密码,MD 5

    public static String buildingName = "default";
    public static String floorName = "default";
    public static String cityName = "beijing";

    public static String wifiDataFileName = "wifi";
    public static String bluetoothFileName = "bluetooth";
    public static String sensorFileName = "sensor";
    public static String zipFileName = "default";
    public static String baseFileName = "default";
    public static String baseFilePath = "IKAPOPOP";

    public static String startTime = "";  //应用启动时间
    public static String exitTime = "";  // 退出应用时间
    public static List<String> operaRecord; // 操作记录, 记录最终生成的zip包名称.
    public static String lat; // 定位点
    public static String lng; // 定位点


    public static final String BASE_FILE_PATH = Environment.getExternalStorageDirectory() + "/nexd/collect/";
    public static final String CONFIG = "config";
    public static final String USER_CONFIG = "user.config";
    public static final String MALL_CONFIG = "mall.config";


    public static boolean wifi = true;
    public static boolean bluetooth = false;
    public static boolean mix = false;

    public static Map<String, UserInfo> user;
    public static ArrayList<ExpandableModel> malls;

    public static MallModel selectedMallModel;
    public static String buildingId;
    public static String mallName;
    public static LiteOrm liteOrm;

    public static final int NO_UPLOAD = 0;
    public static final int UPLOADED = 1;
    private Vibrator vibrator;
    private MySqlLiteOpenHelper dbHelper;
    private static NexdApplication nexdApplication;
    private String DB_PATH;


    public String getDB_PATH() {
        if (FileUtil.checkExist(nexdApplication.getDatabasePath(MySqlLiteOpenHelper.dbName).getAbsolutePath())) {
            return DB_PATH = nexdApplication.getDatabasePath(MySqlLiteOpenHelper.dbName).getAbsolutePath();
        }
        return "";
    }

    public MySqlLiteOpenHelper getDbHelper() {
        if (dbHelper == null) {
            dbHelper = new MySqlLiteOpenHelper(this);
        }

        return dbHelper;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        nexdApplication = this;
        user = new HashMap<String, UserInfo>();
        malls = new ArrayList<ExpandableModel>();
        operaRecord = new ArrayList<String>();
        if (liteOrm == null) {
            liteOrm = LiteOrm.newSingleInstance(this, "nexd.db");
        }
        liteOrm.setDebugged(true); // open the log
        MobclickAgent.setCatchUncaughtExceptions(true);
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        // 设置友盟基础信息appkey 和 Channel Id
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(
                this.getApplicationContext()));
        AnalyticsConfig.setAppkey(this, "56eff2eb67e58e37b40007fe");
        AnalyticsConfig.setChannel("Nexd");
    }

    public static NexdApplication getInstance() {
        return nexdApplication;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        vibrator.cancel();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    @Override
    public void registerComponentCallbacks(ComponentCallbacks callback) {
        super.registerComponentCallbacks(callback);
    }

    @Override
    public void unregisterComponentCallbacks(ComponentCallbacks callback) {
        super.unregisterComponentCallbacks(callback);
    }

    @Override
    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.registerActivityLifecycleCallbacks(callback);
    }

    @Override
    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.unregisterActivityLifecycleCallbacks(callback);
    }

    @Override
    public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
        super.registerOnProvideAssistDataListener(callback);
    }

    @Override
    public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
        super.unregisterOnProvideAssistDataListener(callback);
    }

}
